"cor.dcov.power.study.trial.chisq"=function(nu,n){
##set up our paramters here
d = 3 #dimension
#index of our 'independent' variable
indep.var = 1 
#knowledge of some other variable(s) makes this independent with indep.var
conditionally.independent.var = 2 
#always has some dependency with indep.var
dependent.var = 3
#we'll use one coefficient of dependence for this basic simulation
correlation1 = 0.65
correlation2 = 0.65
#number of samples to generate
#n=1000
#marginals parameters
#mean = 0
#nu=2.1
##end parameter set-up
###################################

omega=matrix(rep(1,d^2),d)

for (j in 1:d){ #columns
	for (k in 1:d){
		if (j == k){
			omega[j,k]=1
		}else{
			if ((j<k & k<3) | (k<j & j < 3)){
				omega[j,k]=0
			}else{
				if((k>j & j>1) | (j>k & k>1)){
					omega[j,k]=correlation1
				}else{
					omega[j,k]=correlation2
				}
			}
		}	
	}
}

#invert
sigma=solve(omega)
sigma.cor = cov2cor(sigma)

#set our correlation matrix here
param=c(sigma.cor[1,2],sigma.cor[1,3],sigma.cor[2,3])
#coupla specifications
#t.copula=tCopula(param,dim=d,dispstr="un",df=3)
normal.copula=normalCopula(param,dim=d,dispstr="un")
#marginal specifications
marginals=rep("chisq",d)
#marginals = rep("t",d)
#marginals = rep("std",d)
#marginals = rep("norm",d)
#more marginal specifications
marginals.params = list(list( df = nu),list( df = nu),list( df = nu))
#marginals.params = list(list(mean = 0, sd =1, nu = nu),list(mean = 0, sd =1, nu = nu),list(mean = 0, sd =1, nu = nu))
#marginals.params = list(list(mean = 0, sd =1),list(mean = 0, sd =1),list(mean = 0, sd =1))
#colupa plus marginals
#mvariate.with.copula = mvdc(t.copula,marginals,marginals.params)
marginals.with.copula = mvdc(normal.copula,marginals,marginals.params)
#generate some samples from the copula
random.multivariate.with.copula = rmvdc(marginals.with.copula,n)
#our 3-d case
x1=random.multivariate.with.copula[,1]
x2=random.multivariate.with.copula[,2]
x3=random.multivariate.with.copula[,3]
#pairs(random.multivariate.with.copula)
x1.reg=lm(x1~x3)
e1=x1.reg$residuals
x2.reg=lm(x2~x3)
e2=x2.reg$residuals
#implied covariance matrix
implied.cov = cov(cbind(x1,x2,x3))
dcov.test.result = dcov.test(e1,e2,R=199) 
distance.covar.p.value = as.numeric(dcov.test.result$p.value);
distance.covar.test.stat = as.numeric(dcov.test.result$statistic);

cor.test = cor.test(e1,e2,method="pearson");
cor.p.value = as.numeric(cor.test$p.value); 
cor.test.stat = as.numeric(cor.test$statistic);
	 return(structure(list(dcov.pvalue = distance.covar.p.value, dcov.teststat = distance.covar.test.stat, 
		  cor.pvalue = cor.p.value, cor.teststat = cor.test.stat,implied.cov=implied.cov)));

}